package com.oa.tools.jobs.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Node;

import com.oa.tools.TkMapper;
import com.oa.tools.entity.OAFlowInstanceWithBLOBs;
import com.oa.tools.entity.ProjectAccount;
import com.oa.tools.jobs.FlowHandlerTaskBase;
import com.oa.tools.utils.XmlUtil;
import com.oa.tools.utils.XmlUtil.XMLHandler;
import com.oa.tools.utils.http.EKPRequestClient;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

public class AddNewFinacialCompanyTask extends FlowHandlerTaskBase{

	public AddNewFinacialCompanyTask(Map<String,TkMapper<?>> daoMap) {
		super(daoMap);
		// TODO Auto-generated constructor stub
		
		this.flowStatus = FlowHandlerTaskBase.FLOW_STATUS_DONE;
		this.templateId = "15ec75886258347e137231346948c2d0";
	}

	@Override
	public String exec(boolean notice, OAFlowInstanceWithBLOBs obj) throws Exception {
		
		String compId = "fd_3597a2ca089bd4";
		
		StringBuilder sb = new StringBuilder();
		final Map<String,Object> resultMap = new HashMap<>();
		String xmlStr = obj.getExtendDataXml();
//		System.out.println(xmlStr);
		XmlUtil.parseXmlWithHandler(xmlStr, "/java/object/void",new XMLHandler() {
			
			@Override
			public void hanle(Node paramNode) throws Exception {
				Node ele1 = paramNode.selectSingleNode("string");
				
				if(ele1.getText().equals(compId)) {
					Node node = paramNode.selectSingleNode("string[2]");
					resultMap.put("company", node.getText());
				}
				
			}
		});
		
		String companyName =  resultMap.get("company").toString();
		
		Example example = new Example(ProjectAccount.class);
		Criteria criteria= example.createCriteria().andEqualTo("fdValidity", (short)1);
		int count = projectAccountDAO.selectCount(new ProjectAccount());
		criteria.andEqualTo("fdName", companyName);/*.andGreaterThan("docCreateTime", new Date(lastestDateMill))*/;
		List<ProjectAccount> objs = this.projectAccountDAO.selectByExample(example);
		if(objs==null || objs.size()==0) {
			count = count+1;
			String fdNumber = "fptt"+count;
			if(count<100) {
				fdNumber = "fptt0"+count;
			}
			EKPRequestClient.addFinacialCompany2Ekp(companyName,fdNumber, count+"");
			sb.append("新增财务发票抬头："+companyName+"-"+fdNumber);
		}
		
		return sb.toString();
	}

	
}
